package com.wngbms.netty._05heartbeat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MainServer {

    public static void main(String[] args) {
        new MainServer().runServer();
    }

    public void runServer() {
        EventLoopGroup bossLoopGroup = new NioEventLoopGroup(2);
        EventLoopGroup workerLoopGroup = new NioEventLoopGroup(4);

        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(bossLoopGroup, workerLoopGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        log.info("initChannel:{}", socketChannel);
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast(new HeartBeatServerHandler());
                    }
                });

        ChannelFuture channelFuture = serverBootstrap.bind(9090);
        channelFuture.syncUninterruptibly();
        Channel channel = channelFuture.channel();
        log.info("server started channel:{}", channel);
    }

}
